/*
 * Copyright@www.it1997.com.
 * @Author: 陈晓晨
 * @Date: 2020/6/6
 * Description:版权所有 QQ:376231445
 */

package com.it1997.it1997admin.utils;

import com.it1997.it1997admin.pojo.Permission;

import java.util.ArrayList;
import java.util.List;

public class TreeUtil {
    public static List<Permission> toTree(List<Permission> treeList, Long pid) {
        List<Permission> retList = new ArrayList<Permission>();
        for (Permission parent : treeList) {
            if (pid.equals(parent.getPid())) {
                retList.add(findChildren(parent, treeList));
            }
        }
        return retList;
    }
    private static Permission findChildren(Permission parent, List<Permission> treeList) {
        for (Permission child : treeList) {
            if (parent.getId().equals(child.getPid())) {
                if (parent.getChild() == null) {
                    parent.setChild(new ArrayList<>());
                }
                parent.getChild().add(findChildren(child, treeList));
            }
        }
        return parent;
    }
}
